Ext.define('jwawa.erp.cfg.entity.EntityDetailModel', {
    extend: 'jwawa.erp.framework.JwawaModel',
    constructor: function(config){
        var me = this;

        Ext.apply(me, config);
        me.callParent(arguments);
    },

    findEntityDetailListByHeaderId: function(entityHeaderId){
        "use strict";
        var me = this,
            entityDetailList;

        Jwawa.framework.JwawaConnection.request({
            url      : '/entity/find-entity-detail-list-by-header-id',
            async    : false,
            jsonData: entityHeaderId,
            success : function(response) {
                entityDetailList = response['data'] || [];

            }
        });

        return entityDetailList;
    },

    getEntityMapByNames: function(entityNames){
        "use strict";
        var me = this,
            entityMap = {},
            entityDetailList;

        if(!Ext.isArray(entityNames) || entityNames.length == 0){
            return entityMap;
        }
        Jwawa.framework.JwawaConnection.request({
            url      : '/entity/find-entity-detail-by-entity-codes',
            async    : false,
            jsonData: entityNames,
            success : function(response) {
                entityDetailList = response['data'];
            }
        });

        Ext.each(entityDetailList, function(entityDetail){
            if(Ext.isEmpty(entityMap[entityDetail['entity_code']])){
                entityMap[entityDetail['entity_code']] = {};
            }
            entityMap[entityDetail['entity_code']][entityDetail['entity_field_name']] =
                entityDetail['entity_field_label'];
        });

        return entityMap;
    }
});